﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.Revit;
using HYFamilyLibDataManager;

namespace YArchitech.HVAC
{
	public class HYDuctAlignment
	{
		private DialogResult IsDistanceOK(string szTilte)
		{
			return MessageBox.Show(szTilte + "值过小（过小有可能连接失败），是否采用推荐值？\n是：采用程序推荐值。\n否：采用设置值。\n取消：退出操作", "盈建科提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
		}

		private double getDuctHeight(Duct duct)
		{
			double result;
			if (new YJKPiping<Duct>(duct).StartConnector.Connector.Shape == ConnectorSharp.Round)
			{
				result = duct.Diameter;
			}
			else
			{
				result = duct.Height;
			}
			return result;
		}

		private double getDuctWidth(Duct duct)
		{
			double result;
			if (new YJKPiping<Duct>(duct).StartConnector.Connector.Shape == ConnectorSharp.Round)
			{
				result = duct.Diameter;
			}
			else
			{
				result = duct.Width;
			}
			return result;
		}

		public int judgeTwoDuctAlignmentModel(Duct duct1, Duct duct2)
		{
			int result = 0;
			Line line = (duct1.Location as LocationCurve).Curve as Line;
			Line line2 = (duct2.Location as LocationCurve).Curve as Line;
			XYZ hyendPoint = line.GetEndPoint(0);
			XYZ hyendPoint2 = line2.GetEndPoint(0);
			double z = hyendPoint.Z;
			double z2 = hyendPoint2.Z;
			double ductHeight = this.getDuctHeight(duct1);
			double ductWidth = this.getDuctWidth(duct1);
			double ductHeight2 = this.getDuctHeight(duct2);
			double ductWidth2 = this.getDuctWidth(duct2);
			double num = (ductWidth > ductWidth2) ? ductWidth2 : ductWidth;
			double num2 = (ductWidth < ductWidth2) ? ductWidth2 : ductWidth;
			double num3 = (ductHeight > ductHeight2) ? ductHeight2 : ductHeight;
			double num4 = (ductHeight < ductHeight2) ? ductHeight2 : ductHeight;
			YJKLine hyline = new YJKLine(line);
			if (Math.Abs(z - z2) < 1E-06)
			{
				if (hyline.IsParallel(line2))
				{
					if (hyline.GetDistance(line2) < 0.0001)
					{
						result = 1;
					}
					else if (Math.Abs(num / 2.0 + hyline.GetDistance(line2) - num2 / 2.0) < 0.0001)
					{
						result = 2;
					}
				}
				else if (Math.Abs(z - z2) < 0.0001)
				{
					result = 9;
				}
			}
			else if (hyline.IsParallel(line2))
			{
				if (Math.Abs(num / 2.0 + Math.Sqrt(hyline.GetDistance(line2) * hyline.GetDistance(line2) - Math.Abs(z - z2) * Math.Abs(z - z2)) - num2 / 2.0) < 0.0001)
				{
					if (Math.Abs(num3 / 2.0 + Math.Abs(z - z2) - num4 / 2.0) < 0.0001)
					{
						if ((z < z2 && this.getDuctHeight(duct1) > this.getDuctHeight(duct2)) || (z > z2 && this.getDuctHeight(duct1) < this.getDuctHeight(duct2)))
						{
							result = 3;
						}
						else if ((z < z2 && this.getDuctHeight(duct1) < this.getDuctHeight(duct2)) || (z > z2 && this.getDuctHeight(duct1) > this.getDuctHeight(duct2)))
						{
							result = 4;
						}
					}
					else
					{
						result = 2;
					}
				}
				else if (Math.Abs(num3 / 2.0 + Math.Abs(z - z2) - num4 / 2.0) < 0.0001)
				{
					if ((z < z2 && this.getDuctHeight(duct1) > this.getDuctHeight(duct2)) || (z > z2 && this.getDuctHeight(duct1) < this.getDuctHeight(duct2)))
					{
						result = 5;
					}
					else if ((z < z2 && this.getDuctHeight(duct1) < this.getDuctHeight(duct2)) || (z > z2 && this.getDuctHeight(duct1) > this.getDuctHeight(duct2)))
					{
						result = 6;
					}
				}
			}
			else if (Math.Abs(num3 / 2.0 + hyline.GetDPLineDistance(line2) - num4 / 2.0) < 0.0001)
			{
				if ((z < z2 && this.getDuctHeight(duct1) > this.getDuctHeight(duct2)) || (z > z2 && this.getDuctHeight(duct1) < this.getDuctHeight(duct2)))
				{
					result = 7;
				}
				else if ((z < z2 && this.getDuctHeight(duct1) < this.getDuctHeight(duct2)) || (z > z2 && this.getDuctHeight(duct1) > this.getDuctHeight(duct2)))
				{
					result = 8;
				}
			}
			return result;
		}

		private List<Duct> OrderDuctPosition(List<Duct> ducts, ref List<XYZ> xyzs)
		{
			List<Duct> list = new List<Duct>();
			List<XYZ> list2 = new List<XYZ>();
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
			if (3 == ducts.Count)
			{
				if (hypiping.IsPipingOnLine(hypiping2.Piping) || hypiping.IsParallel(hypiping2.Piping))
				{
					list.Add(hypiping3.Piping);
					list.Add(hypiping.Piping);
					list.Add(hypiping2.Piping);
					list2.Add(hypiping3.GlobalPoint);
					list2.Add(hypiping.GlobalPoint);
					list2.Add(hypiping2.GlobalPoint);
				}
				else if (hypiping.IsPipingOnLine(hypiping3.Piping) || hypiping.IsParallel(hypiping3.Piping))
				{
					list.Add(hypiping2.Piping);
					list.Add(hypiping.Piping);
					list.Add(hypiping3.Piping);
					list2.Add(hypiping2.GlobalPoint);
					list2.Add(hypiping.GlobalPoint);
					list2.Add(hypiping3.GlobalPoint);
				}
				else
				{
					list = ducts;
					list2 = xyzs;
				}
			}
			else if (4 == ducts.Count)
			{
				YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(ducts[3]);
				if (hypiping.IsPipingOnLine(hypiping2.Piping))
				{
					list = ducts;
					list2 = xyzs;
				}
				else if (hypiping.IsPipingOnLine(hypiping3.Piping))
				{
					list.Add(hypiping.Piping);
					list.Add(hypiping3.Piping);
					list.Add(hypiping2.Piping);
					list.Add(hypiping4.Piping);
					list2.Add(hypiping.GlobalPoint);
					list2.Add(hypiping3.GlobalPoint);
					list2.Add(hypiping2.GlobalPoint);
					list2.Add(hypiping4.GlobalPoint);
				}
				else if (hypiping.IsPipingOnLine(hypiping4.Piping))
				{
					list.Add(hypiping.Piping);
					list.Add(hypiping4.Piping);
					list.Add(hypiping2.Piping);
					list.Add(hypiping3.Piping);
					list2.Add(hypiping.GlobalPoint);
					list2.Add(hypiping4.GlobalPoint);
					list2.Add(hypiping2.GlobalPoint);
					list2.Add(hypiping3.GlobalPoint);
				}
				else
				{
					list = ducts;
					list2 = xyzs;
				}
			}
			xyzs = list2;
			return list;
		}

		private void AlignmentDuctConnect(List<Duct> listDuct2, List<XYZ> listXYZ, ElementNode en)
		{
            if ((int)YJKRevitTools.GetConnectorAll(listDuct2.ElementAt(0)).ElementAt(0).Shape != 2)
			{
				ConnectDuctManage connectDuctManage = new ConnectDuctManage();
				List<string> listName = new List<string>();
				HYDuctConnect hyductConnect = new HYDuctConnect();
				List<Duct> list = new List<Duct>();
				List<Duct> list2 = new List<Duct>();
				List<XYZ> list3 = new List<XYZ>();
				list = this.OrderDuctPosition(listDuct2, ref listXYZ);
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(list.ElementAt(0));
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(list.ElementAt(1));
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(list.ElementAt(2));
				double heightOrDiameter = hypiping.GetHeightOrDiameter();
				double heightOrDiameter2 = hypiping2.GetHeightOrDiameter();
				double heightOrDiameter3 = hypiping3.GetHeightOrDiameter();
				hypiping.GetWeightOrDiameter();
				hypiping2.GetWeightOrDiameter();
				hypiping3.GetWeightOrDiameter();
				double ductEQDiameter = hypiping.GetDuctEQDiameter();
				double ductEQDiameter2 = hypiping2.GetDuctEQDiameter();
				double ductEQDiameter3 = hypiping3.GetDuctEQDiameter();
				if (list.Count == 3 && listXYZ.Count == 3)
				{
					if (hypiping.IsRect() && hypiping2.IsRect() && hypiping3.IsRect())
					{
						if (!hypiping.IsPipingOnLine(hypiping2.Piping) && !hypiping.IsParallel(hypiping2.Piping) && !hypiping.IsPipingOnLine(hypiping3.Piping) && !hypiping.IsParallel(hypiping3.Piping) && !hypiping3.IsPipingOnLine(hypiping2.Piping) && !hypiping3.IsParallel(hypiping2.Piping))
						{
							ConnectDuctStyle connStyle = ConnectDuctStyle.CommonTee;
							listName = connectDuctManage.GetFittingNames(connStyle);
							string text = "";
							hyductConnect.NewTeeFittingY(list, listXYZ, listName, ref text);
						}
						else
						{
							list2.Add(list.ElementAt(1));
							list2.Add(list.ElementAt(2));
							list2.Add(list.ElementAt(0));
							list3.Add(listXYZ.ElementAt(1));
							list3.Add(listXYZ.ElementAt(2));
							list3.Add(listXYZ.ElementAt(0));
							string a = "T";
							List<Duct> list4 = this.orderDuctMainT(en, list2, ref list3, ref a);
							if (a == "T")
							{
								hypiping = new YJKPiping<Duct>(list4.ElementAt(0));
								hypiping2 = new YJKPiping<Duct>(list4.ElementAt(1));
								hypiping3 = new YJKPiping<Duct>(list4.ElementAt(2));
								heightOrDiameter = hypiping.GetHeightOrDiameter();
								heightOrDiameter2 = hypiping2.GetHeightOrDiameter();
								heightOrDiameter3 = hypiping3.GetHeightOrDiameter();
								hypiping.GetWeightOrDiameter();
								hypiping2.GetWeightOrDiameter();
								hypiping3.GetWeightOrDiameter();
								ductEQDiameter = hypiping.GetDuctEQDiameter();
								ductEQDiameter2 = hypiping2.GetDuctEQDiameter();
								ductEQDiameter3 = hypiping3.GetDuctEQDiameter();
								if (heightOrDiameter == heightOrDiameter2 && heightOrDiameter2 == heightOrDiameter3 && heightOrDiameter == heightOrDiameter3)
								{
									ConnectDuctStyle connStyle = ConnectDuctStyle.RightAngleTee;
									listName = connectDuctManage.GetFittingNames(connStyle);
									string text2 = "";
									hyductConnect.NewTeeFittingT(list4, list3, listName, ref text2);
								}
								else
								{
									ConnectDuctStyle connStyle = ConnectDuctStyle.IntubationTee;
									listName = connectDuctManage.GetFittingNames(connStyle);
									string text3 = "";
									hyductConnect.NewTeeFittingT(list4, list3, listName, ref text3);
								}
							}
							else if (a == "F")
							{
								hypiping = new YJKPiping<Duct>(list4.ElementAt(0));
								hypiping2 = new YJKPiping<Duct>(list4.ElementAt(1));
								hypiping3 = new YJKPiping<Duct>(list4.ElementAt(2));
								heightOrDiameter = hypiping.GetHeightOrDiameter();
								heightOrDiameter2 = hypiping2.GetHeightOrDiameter();
								heightOrDiameter3 = hypiping3.GetHeightOrDiameter();
								hypiping.GetWeightOrDiameter();
								hypiping2.GetWeightOrDiameter();
								hypiping3.GetWeightOrDiameter();
								ductEQDiameter = hypiping.GetDuctEQDiameter();
								ductEQDiameter2 = hypiping2.GetDuctEQDiameter();
								ductEQDiameter3 = hypiping3.GetDuctEQDiameter();
								if (heightOrDiameter == heightOrDiameter3 && heightOrDiameter == heightOrDiameter2 && heightOrDiameter3 == heightOrDiameter2)
								{
									ConnectDuctStyle connStyle = ConnectDuctStyle.TShapeTee;
									listName = connectDuctManage.GetFittingNames(connStyle);
									string text4 = "";
									hyductConnect.NewTeeFittingF(list4, list3, listName, ref text4);
								}
								else
								{
									ConnectDuctStyle connStyle = ConnectDuctStyle.YShapeTee;
									listName = connectDuctManage.GetFittingNames(connStyle);
									string text5 = "";
									hyductConnect.NewTeeFittingF(list4, list3, listName, ref text5);
								}
							}
						}
					}
					else if (!hypiping.IsRect() && !hypiping2.IsRect() && !hypiping3.IsRect())
					{
						if (!hypiping.IsPipingOnLine(hypiping2.Piping) && !hypiping.IsParallel(hypiping2.Piping) && !hypiping.IsPipingOnLine(hypiping3.Piping) && !hypiping.IsParallel(hypiping3.Piping) && !hypiping3.IsPipingOnLine(hypiping2.Piping) && !hypiping3.IsParallel(hypiping2.Piping))
						{
							ConnectDuctStyle connStyle = ConnectDuctStyle.CommonTeeRound;
							listName = connectDuctManage.GetFittingNames(connStyle);
							string text6 = "";
							hyductConnect.NewTeeFittingY(list, listXYZ, listName, ref text6);
						}
						else
						{
							YJKLine hyline = new YJKLine(hypiping.GetLocationCurve() as Line);
							YJKLine hyline2 = new YJKLine(hypiping2.GetLocationCurve() as Line);
							if (hyline.IsVertical(hyline2.Line))
							{
								ConnectDuctStyle connStyle = ConnectDuctStyle.RightAngleTeeRound;
								listName = connectDuctManage.GetFittingNames(connStyle);
								string text7 = "";
								hyductConnect.NewTeeFittingT(list, listXYZ, listName, ref text7);
							}
							else
							{
								ConnectDuctStyle connStyle = ConnectDuctStyle.SlantingTeeRound;
								listName = connectDuctManage.GetFittingNames(connStyle);
								string text8 = "";
								hyductConnect.NewTeeFittingT(list, listXYZ, listName, ref text8);
							}
						}
					}
				}
				if (list.Count == 4 && listXYZ.Count == 4)
				{
					double ductEQDiameter4 = new YJKPiping<Duct>(list.ElementAt(3)).GetDuctEQDiameter();
					List<double> list5 = new List<double>();
					list5.Add(ductEQDiameter);
					list5.Add(ductEQDiameter2);
					list5.Add(ductEQDiameter3);
					list5.Add(ductEQDiameter4);
					List<Duct> list6 = new List<Duct>();
					List<XYZ> list7 = new List<XYZ>();
					if (ductEQDiameter4 == list5.Max())
					{
						list6.Add(list.ElementAt(3));
						list6.Add(list.ElementAt(2));
						list6.Add(list.ElementAt(1));
						list6.Add(list.ElementAt(0));
						list7.Add(listXYZ.ElementAt(3));
						list7.Add(listXYZ.ElementAt(2));
						list7.Add(listXYZ.ElementAt(1));
						list7.Add(listXYZ.ElementAt(0));
					}
					else if (ductEQDiameter3 == list5.Max())
					{
						list6.Add(list.ElementAt(2));
						list6.Add(list.ElementAt(3));
						list6.Add(list.ElementAt(1));
						list6.Add(list.ElementAt(0));
						list7.Add(listXYZ.ElementAt(2));
						list7.Add(listXYZ.ElementAt(3));
						list7.Add(listXYZ.ElementAt(1));
						list7.Add(listXYZ.ElementAt(0));
					}
					else if (ductEQDiameter2 == list5.Max())
					{
						list6.Add(list.ElementAt(1));
						list6.Add(list.ElementAt(0));
						list6.Add(list.ElementAt(2));
						list6.Add(list.ElementAt(3));
						list7.Add(listXYZ.ElementAt(1));
						list7.Add(listXYZ.ElementAt(0));
						list7.Add(listXYZ.ElementAt(2));
						list7.Add(listXYZ.ElementAt(3));
					}
					else if (ductEQDiameter == list5.Max())
					{
						list6.Add(list.ElementAt(0));
						list6.Add(list.ElementAt(1));
						list6.Add(list.ElementAt(2));
						list6.Add(list.ElementAt(3));
						list7.Add(listXYZ.ElementAt(0));
						list7.Add(listXYZ.ElementAt(1));
						list7.Add(listXYZ.ElementAt(2));
						list7.Add(listXYZ.ElementAt(3));
					}
					string text9 = "";
					ConnectDuctStyle connStyle;
					if (hypiping.IsRect() && hypiping2.IsRect() && hypiping3.IsRect())
					{
						connStyle = ConnectDuctStyle.RoundAngleCross;
					}
					else
					{
						connStyle = ConnectDuctStyle.IntubationCrossRound;
					}
					listName = connectDuctManage.GetFittingNames(connStyle);
					hyductConnect.NewCrossFitting(list6, list7, listName, ref text9);
				}
			}
		}

		private List<Duct> orderDuctMainT(ElementNode en, List<Duct> listDuct, ref List<XYZ> listXYZ, ref string style)
		{
			List<Duct> list = new List<Duct>();
			List<XYZ> list2 = new List<XYZ>();
			Duct duct = null;
			if (en.Preview.Element is Duct)
			{
				duct = (en.Preview.Element as Duct);
			}
			else if (en.Preview.Preview.Element is Duct)
			{
				duct = (en.Preview.Preview.Element as Duct);
			}
			if (duct == null)
			{
				return listDuct;
			}
			if (duct.Id == listDuct.ElementAt(0).Id)
			{
				style = "T";
				return listDuct;
			}
			if (duct.Id == listDuct.ElementAt(1).Id)
			{
				list.Add(listDuct.ElementAt(1));
				list.Add(listDuct.ElementAt(0));
				list.Add(listDuct.ElementAt(2));
				list2.Add(listXYZ.ElementAt(1));
				list2.Add(listXYZ.ElementAt(0));
				list2.Add(listXYZ.ElementAt(2));
				style = "T";
			}
			else
			{
				list.Add(listDuct.ElementAt(2));
				list.Add(listDuct.ElementAt(0));
				list.Add(listDuct.ElementAt(1));
				list2.Add(listXYZ.ElementAt(2));
				list2.Add(listXYZ.ElementAt(0));
				list2.Add(listXYZ.ElementAt(1));
				style = "F";
			}
			listXYZ = list2;
			return list;
		}

		private List<Duct> orderDuctMainF(ElementNode en, List<Duct> listDuct)
		{
			return new List<Duct>();
		}

		private FamilySymbol getDuctAlimentFittingFS(Autodesk.Revit.DB.Document doc, int style)
		{
			FamilySymbol result = null;
			HVACDataReader hvacdataReader = new HVACDataReader(doc);
			HVACUserSetting hvacuserSetting = new HVACUserSetting();
			string[] array = new string[4];
			array = hvacuserSetting.GetDuctAlignmentPipingFittingText();
			DbFamily dbFamily = null;
			foreach (DbFamily dbFamily2 in hvacdataReader.GetFamilyAllInfoList("矩形三通"))
			{
				if (dbFamily2.Name == array[0] && style == 1)
				{
					dbFamily = dbFamily2;
					break;
				}
				if (dbFamily2.Name == array[1] && style == 2)
				{
					dbFamily = dbFamily2;
					break;
				}
			}
			foreach (DbFamily dbFamily3 in hvacdataReader.GetFamilyAllInfoList("矩形四通"))
			{
				if (dbFamily3.Name == array[2] && style == 3)
				{
					dbFamily = dbFamily3;
					break;
				}
				if (dbFamily3.Name == array[3] && style == 4)
				{
					dbFamily = dbFamily3;
					break;
				}
			}
			using (List<FamilySymbol>.Enumerator enumerator2 = dbFamily.LoadInSubTrans(doc).GetSymbols().GetEnumerator())
			{
				if (enumerator2.MoveNext())
				{
					result = enumerator2.Current;
				}
			}
			return result;
		}

		public bool DuctAlignmentTop(double ducttop, IList<Duct> ducts)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (ducts.Count < 1)
				{
					throw new System.ArgumentNullException("ducts");
				}
				Document document = ducts[0].Document;
				YJKPiping<Duct>[] array = new YJKPiping<Duct>[ducts.Count];
				for (int i = 0; i < ducts.Count; i++)
				{
					array[i] = new YJKPiping<Duct>(ducts[i]);
				}
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				for (int j = 0; j < array.Count<YJKPiping<Duct>>(); j++)
				{
					double num = array[j].GetHeightOrDiameter() / 2.0;
					double offSet = ducttop - num;
					array[j].SetOffSet(offSet);
				}
				bool flag = true;
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				result = false;
			}
			finally
			{
				if (1 == (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool DuctAlignmentMiddle(double duct, IList<Duct> ducts)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (ducts.Count < 1)
				{
					throw new System.ArgumentNullException("ducts");
				}
				Document document = ducts[0].Document;
				YJKPiping<Duct>[] array = new YJKPiping<Duct>[ducts.Count];
				for (int i = 0; i < ducts.Count; i++)
				{
					array[i] = new YJKPiping<Duct>(ducts[i]);
				}
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				for (int j = 0; j < array.Count<YJKPiping<Duct>>(); j++)
				{
					array[j].SetOffSet(duct);
				}
				bool flag = true;
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				result = false;
			}
			finally
			{
				if (1 == (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool DuctAlignmentBottom(double bottomHeight, IList<Duct> ducts)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (ducts.Count < 1)
				{
					throw new System.ArgumentNullException("ducts");
				}
				Document document = ducts[0].Document;
				YJKPiping<Duct>[] array = new YJKPiping<Duct>[ducts.Count];
				for (int i = 0; i < ducts.Count; i++)
				{
					array[i] = new YJKPiping<Duct>(ducts[i]);
				}
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				for (int j = 0; j < array.Count<YJKPiping<Duct>>(); j++)
				{
					double num = array[j].GetHeightOrDiameter() / 2.0;
					double offSet = bottomHeight + num;
					array[j].SetOffSet(offSet);
				}
				bool flag = true;
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				result = false;
			}
			finally
			{
				if (1 == (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public List<Duct> GetNeedAlignmentDucts(Element elem)
		{
			List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(elem);
			List<Duct> list = new List<Duct>();
			foreach (Connector connector in elementAllConnector)
			{
				Connector connectConnector = YJKRevitTools.GetConnectConnector(connector);
				if (connectConnector != null)
				{
					if (connectConnector.Owner is Duct)
					{
						list.Add(connectConnector.Owner as Duct);
					}
					else
					{
						foreach (Connector connector2 in YJKRevitTools.GetElementAllConnector(connectConnector.Owner))
						{
							if (connector2 != connectConnector && YJKRevitTools.GetConnectConnector(connector2) != null && YJKRevitTools.GetConnectConnector(connector2).Owner is Duct)
							{
								list.Add(YJKRevitTools.GetConnectConnector(connector2).Owner as Duct);
							}
						}
					}
				}
			}
			return list;
		}

		private void deleteElementConnectorFiting(Autodesk.Revit.DB.Document doc, Element elem)
		{
			List<Element> list = new List<Element>();
			list.Add(elem);
			new List<Connector>();
			foreach (Connector connector in YJKRevitTools.GetElementAllConnector(elem))
			{
				Element owner = YJKRevitTools.GetConnectConnector(connector).Owner;
				if (owner != null && owner is FamilyInstance && owner.Category.Id.IntegerValue == -2008010)
				{
					list.Add(owner);
				}
			}
			foreach (Element elem2 in list)
			{
				doc.DeleteElement(elem2);
			}
		}

		private void twoDuctConnector(List<Duct> list, XYZ point)
		{
			if (list.Count == 2)
			{
				Duct duct = list.ElementAt(0);
				Duct piping = list.ElementAt(1);
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(piping);
				Connector closeConnector = hypiping.GetCloseConnector(point, false);
				Connector closeConnector2 = hypiping2.GetCloseConnector(point, false);
				if (hypiping.IsParallel(hypiping2.Piping))
				{
					duct.Document.Create.NewTransitionFitting(closeConnector, closeConnector2);
					return;
				}
				duct.Document.Create.NewElbowFitting(closeConnector, closeConnector2);
			}
		}

		public void DuctBranchAlignment(Duct ductStart, int model, double high)
		{
			Document document = ductStart.Document;
			SearchBranch searchBranch = new SearchBranch(ductStart);
			searchBranch.DoSearch();
			ElementNode listRoot = searchBranch.GetListRoot();
			List<ElementNode> list = new List<ElementNode>();
			this.GetAllElemNode(listRoot, ref list);
			new HYDuctConnect();
			if (list.Count == 0)
			{
				List<Duct> list2 = new List<Duct>();
				list2.Add(ductStart);
				if (model == 1)
				{
					this.DuctAlignmentTop(high, list2);
				}
				if (model == 2)
				{
					this.DuctAlignmentMiddle(high, list2);
				}
				if (model == 3)
				{
					this.DuctAlignmentBottom(high, list2);
					return;
				}
			}
			else
			{
				foreach (ElementNode elementNode in list)
				{
					Element element = elementNode.Element;
					XYZ point = (element.Location as LocationPoint).Point;
					List<Duct> needAlignmentDucts = this.GetNeedAlignmentDucts(element);
					if (needAlignmentDucts.Count >= 2)
					{
						this.deleteElementConnectorFiting(document, element);
					}
					List<XYZ> list3 = new List<XYZ>();
					foreach (Duct duct in needAlignmentDucts)
					{
						List<Connector> connectorEnd = YJKRevitTools.GetConnectorEnd(duct);
						if (connectorEnd.Count == 2)
						{
							if (connectorEnd.ElementAt(0).Origin.DistanceTo(point) < connectorEnd.ElementAt(1).Origin.DistanceTo(point))
							{
								list3.Add(connectorEnd.ElementAt(0).Origin);
							}
							else
							{
								list3.Add(connectorEnd.ElementAt(1).Origin);
							}
						}
					}
					if (model == 1)
					{
						this.DuctAlignmentTop(high, needAlignmentDucts);
						if (needAlignmentDucts.Count == 2)
						{
							this.twoDuctConnector(needAlignmentDucts, point);
						}
						if (needAlignmentDucts.Count == 3)
						{
							this.AlignmentDuctConnect(needAlignmentDucts, list3, elementNode);
						}
						if (needAlignmentDucts.Count == 4)
						{
							this.AlignmentDuctConnect(needAlignmentDucts, list3, elementNode);
						}
					}
					if (model == 2)
					{
						this.DuctAlignmentMiddle(high, needAlignmentDucts);
						if (needAlignmentDucts.Count == 2)
						{
							this.twoDuctConnector(needAlignmentDucts, point);
						}
						if (needAlignmentDucts.Count == 3)
						{
							this.AlignmentDuctConnect(needAlignmentDucts, list3, elementNode);
						}
						if (needAlignmentDucts.Count == 4)
						{
							this.AlignmentDuctConnect(needAlignmentDucts, list3, elementNode);
						}
					}
					if (model == 3)
					{
						this.DuctAlignmentBottom(high, needAlignmentDucts);
						if (needAlignmentDucts.Count == 2)
						{
							this.twoDuctConnector(needAlignmentDucts, point);
						}
						if (needAlignmentDucts.Count == 3)
						{
							this.AlignmentDuctConnect(needAlignmentDucts, list3, elementNode);
						}
						if (needAlignmentDucts.Count == 4)
						{
							this.AlignmentDuctConnect(needAlignmentDucts, list3, elementNode);
						}
					}
					document.Regenerate();
				}
			}
		}

		private List<ElementNode> GetAllElemNode(ElementNode eNode, ref List<ElementNode> listENode)
		{
			int integerValue = eNode.Element.Category.Id.IntegerValue;
			if (eNode == null)
			{
				return listENode;
			}
			if (integerValue == -2008010)
			{
				List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(eNode.Element);
				bool flag = true;
				using (List<Connector>.Enumerator enumerator = elementAllConnector.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						if (enumerator.Current.CoordinateSystem.BasisZ.Z != 0.0)
						{
							flag = false;
							return listENode;
						}
					}
				}
				if (!flag)
				{
					goto IL_146;
				}
				YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
				int count = YJKRevitTools.GetElementAllConnector(eNode.Element).Count;
				List<Connector> elementAllConnector2 = YJKRevitTools.GetElementAllConnector(eNode.Element);
				bool flag2 = false;
				bool flag3 = false;
				if (count == 2 && elementAllConnector2.ElementAt(0).IsConnected && elementAllConnector2.ElementAt(1).IsConnected)
				{
					flag2 = hypipeConnectorHelper.twoConnectorIsAlign(elementAllConnector2.ElementAt(0), elementAllConnector2.ElementAt(1));
					if (flag2 && YJKRevitTools.GetConnectConnector(elementAllConnector2.ElementAt(0)).Owner is Duct && YJKRevitTools.GetConnectConnector(elementAllConnector2.ElementAt(1)).Owner is Duct)
					{
						flag3 = true;
					}
				}
				if (count == 3 || count == 4 || !flag2 || flag3)
				{
					listENode.Add(eNode);
				}
			}
			IL_146:
			if (eNode.Next != null)
			{
				this.GetAllElemNode(eNode.Next, ref listENode);
			}
			if (eNode.SubElementNode != null)
			{
				foreach (ElementNode eNode2 in eNode.SubElementNode)
				{
					this.GetAllElemNode(eNode2, ref listENode);
				}
			}
			return listENode;
		}

		public void DuctBranchAlignment(Duct ductStart, Line line, XYZ xyz)
		{
			SearchBranch searchBranch = new SearchBranch(ductStart);
			searchBranch.DoSearch();
			ElementNode listRoot = searchBranch.GetListRoot();
			List<ElementNode> list = new List<ElementNode>();
			this.GetAllElemNodeReference(listRoot, ref list);
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ductStart);
			XYZ endPoint = hypiping.GetLocationCurve().GetEndPoint(0);
			XYZ endPoint2 = hypiping.GetLocationCurve().GetEndPoint(1);
			XYZ start = new XYZ(endPoint.X, endPoint.Y, 0.0);
			XYZ end = new XYZ(endPoint2.X, endPoint2.Y, 0.0);
			YJKLine hyline = new YJKLine(start, end);
			XYZ xyz2 = new XYZ(xyz.X, xyz.Y, 0.0);
			XYZ project = hyline.GetProject(xyz2);
			XYZ xyz3 = (xyz2 - project).Normalize();
			if (list.Count == 0)
			{
				this.DuctAlignmentEdge(line, ductStart, xyz);
				return;
			}
			foreach (ElementNode elementNode in list)
			{
				Element element = elementNode.Element;
				XYZ point = (element.Location as LocationPoint).Point;
				List<Duct> needAlignmentDucts = this.GetNeedAlignmentDucts(element);
				this.deleteElementConnectorFiting(ductStart.Document, element);
				List<XYZ> list2 = new List<XYZ>();
				foreach (Duct duct in needAlignmentDucts)
				{
					List<Connector> connectorEnd = YJKRevitTools.GetConnectorEnd(duct);
					if (connectorEnd.Count == 2)
					{
						if (connectorEnd.ElementAt(0).Origin.DistanceTo(point) < connectorEnd.ElementAt(1).Origin.DistanceTo(point))
						{
							list2.Add(connectorEnd.ElementAt(0).Origin);
						}
						else
						{
							list2.Add(connectorEnd.ElementAt(1).Origin);
						}
					}
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct);
					XYZ endPoint3 = hypiping2.GetLocationCurve().GetEndPoint(0);
					XYZ endPoint4 = hypiping2.GetLocationCurve().GetEndPoint(1);
					XYZ start2 = new XYZ(endPoint3.X, endPoint3.Y, 0.0);
					XYZ end2 = new XYZ(endPoint4.X, endPoint4.Y, 0.0);
					YJKLine hyline2 = new YJKLine(start2, end2);
					if (hypiping2.IsParallel(ductStart))
					{
						if (hypiping2.Piping != ductStart)
						{
							xyz = (hyline2.Line.GetEndPoint(0) + hyline2.Line.GetEndPoint(1)) / 2.0 + xyz3 * hypiping2.GetWeightOrDiameter() / 2.0;
						}
						this.DuctAlignmentEdge(line, duct, xyz);
						ductStart.Document.Regenerate();
					}
				}
				if (needAlignmentDucts.Count == 2)
				{
					this.twoDuctConnector(needAlignmentDucts, point);
				}
			}
		}

		private List<ElementNode> GetAllElemNodeReference(ElementNode eNode, ref List<ElementNode> listENode)
		{
			YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
			int integerValue = eNode.Element.Category.Id.IntegerValue;
			if (eNode == null)
			{
				return listENode;
			}
			if (integerValue == -2008010)
			{
				List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(eNode.Element);
				bool flag = true;
				bool flag2 = false;
				int num = 0;
				foreach (Connector connector in elementAllConnector)
				{
					num++;
					if (connector.CoordinateSystem.BasisZ.Z != 0.0)
					{
						flag = false;
						return listENode;
					}
				}
				if (num == 2)
				{
					flag2 = hypipeConnectorHelper.twoConnectorIsAlign(elementAllConnector.ElementAt(0), elementAllConnector.ElementAt(1));
				}
				if (num > 2 || !flag2)
				{
					return listENode;
				}
				if (flag && flag2)
				{
					listENode.Add(eNode);
				}
			}
			if (eNode.Next != null)
			{
				this.GetAllElemNodeReference(eNode.Next, ref listENode);
			}
			if (eNode.SubElementNode != null)
			{
				foreach (ElementNode eNode2 in eNode.SubElementNode)
				{
					this.GetAllElemNodeReference(eNode2, ref listENode);
				}
			}
			return listENode;
		}

		public bool DuctAlignmentCentLine(Duct duct, double high)
		{
			bool flag = false;
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (duct == null)
				{
					throw new System.ArgumentNullException("ducts");
				}
				Document document = duct.Document;
				new YJKPiping<Duct>(duct);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				flag = false;
				result = flag;
			}
			finally
			{
				if (1 == (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool DuctAlignmentEdge(Line line, Duct listDuct, XYZ xyz)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (listDuct == null)
				{
					throw new System.ArgumentNullException("ducts");
				}
				subTransaction = new SubTransaction(listDuct.Document);
				subTransaction.Start();
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(listDuct);
				XYZ xyz2 = (hypiping.StartConnector.Origin - hypiping.EndConnector.Origin).Normalize();
				Line line2 = new YJKLine(xyz, xyz + 1.0 * xyz2).Line;
				YJKLine hyline = new YJKLine(line2.GetEndPoint(0), line2.GetEndPoint(1));
				YJKLine hyline2 = new YJKLine(line);
				XYZ endPoint = hyline.Line.GetEndPoint(0);
				XYZ endPoint2 = hyline.Line.GetEndPoint(1);
				XYZ endPoint3 = hyline2.Line.GetEndPoint(0);
				XYZ endPoint4 = hyline2.Line.GetEndPoint(1);
				XYZ start = new XYZ(endPoint.X, endPoint.Y, 0.0);
				XYZ end = new XYZ(endPoint2.X, endPoint2.Y, 0.0);
				XYZ start2 = new XYZ(endPoint3.X, endPoint3.Y, 0.0);
				XYZ end2 = new XYZ(endPoint4.X, endPoint4.Y, 0.0);
				YJKLine hyline3 = new YJKLine(start, end);
				YJKLine hyline4 = new YJKLine(start2, end2);
				double xydistance = hyline3.GetXYDistance(hyline4.Line);
				XYZ xyvector = hyline4.GetXYVector(hyline3.Line);
				bool flag = (hypiping.Piping.Location as LocationCurve).Move(xydistance * xyvector);
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				result = false;
			}
			finally
			{
				if (1 == (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool DuctAlignmentTop(Duct duct, IList<Duct> ducts)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (ducts.Count < 1 || duct == null)
				{
					throw new System.ArgumentNullException("ducts");
				}
				Document document = ducts[0].Document;
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				double offSet = hypiping.GetOffSet();
				double num = hypiping.GetHeightOrDiameter() / 2.0;
				YJKPiping<Duct>[] array = new YJKPiping<Duct>[ducts.Count];
				for (int i = 0; i < ducts.Count; i++)
				{
					array[i] = new YJKPiping<Duct>(ducts[i]);
				}
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				for (int j = 0; j < array.Count<YJKPiping<Duct>>(); j++)
				{
					double offSet2 = offSet + (num - array[j].GetHeightOrDiameter() / 2.0);
					array[j].SetOffSet(offSet2);
				}
				bool flag = true;
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				result = false;
			}
			finally
			{
				if (1 == (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool DuctAlignmentMiddle(Duct duct, IList<Duct> ducts)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (ducts.Count < 1 || duct == null)
				{
					throw new System.ArgumentNullException("ducts");
				}
				Document document = ducts[0].Document;
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				double offSet = hypiping.GetOffSet();
				double num = hypiping.GetHeightOrDiameter() / 2.0;
				YJKPiping<Duct>[] array = new YJKPiping<Duct>[ducts.Count];
				for (int i = 0; i < ducts.Count; i++)
				{
					array[i] = new YJKPiping<Duct>(ducts[i]);
				}
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				for (int j = 0; j < array.Count<YJKPiping<Duct>>(); j++)
				{
					double offSet2 = offSet;
					array[j].SetOffSet(offSet2);
				}
				bool flag = true;
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				result = false;
			}
			finally
			{
				if (1 == (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool DuctAlignmentBottom(Duct duct, IList<Duct> ducts)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (ducts.Count < 1 || duct == null)
				{
					throw new System.ArgumentNullException("ducts");
				}
				Document document = ducts[0].Document;
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				double offSet = hypiping.GetOffSet();
				double num = hypiping.GetHeightOrDiameter() / 2.0;
				YJKPiping<Duct>[] array = new YJKPiping<Duct>[ducts.Count];
				for (int i = 0; i < ducts.Count; i++)
				{
					array[i] = new YJKPiping<Duct>(ducts[i]);
				}
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				for (int j = 0; j < array.Count<YJKPiping<Duct>>(); j++)
				{
					double offSet2 = offSet - (num - array[j].GetHeightOrDiameter() / 2.0);
					array[j].SetOffSet(offSet2);
				}
				bool flag = true;
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				result = false;
			}
			finally
			{
				if (1 == (int)subTransaction.GetStatus())
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		public bool DuctAlignmentWall(Duct duct, XYZ ductGlobal, XYZ wallGlobal, double width)
		{
			bool result = false;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct, ductGlobal);
			XYZ xyz = (hypiping.StartConnector.Origin - hypiping.EndConnector.Origin).Normalize();
			Line curve = Line.CreateBound(hypiping.GlobalPoint, hypiping.GlobalPoint + 1.0 * xyz);
			YJKLine hyline = new YJKLine(curve.GetEndPoint(0), curve.GetEndPoint(1));
			Curve wallGlobalEdge = this.GetWallGlobalEdge(duct, wallGlobal);
			YJKLine hyline2 = new YJKLine(wallGlobalEdge.GetEndPoint(0), wallGlobalEdge.GetEndPoint(1));
			double xydistance = hyline2.GetXYDistance(hyline.Line);
			XYZ xyvector = hyline2.GetXYVector(hyline.Line);
			if ((hypiping.Piping.Location as LocationCurve).Move((xydistance - width) * xyvector))
			{
				result = true;
			}
			return result;
		}

		private Curve GetWallGlobalEdge(Wall wall, XYZ wallGlobal)
		{
			Curve result = null;
			double num = wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).AsDouble();
			XYZ xyz = new XYZ(wallGlobal.X, wallGlobal.Y, num);
			Options options = new Options();
			options.ComputeReferences = true;
			foreach (GeometryObject geometryObject in new YJKElement(wall).GetGeometry(options))
			{
				if (geometryObject is Solid)
				{
					foreach (object obj in (geometryObject as Solid).Edges)
					{
						Edge edge = (Edge)obj;
						if (edge.AsCurve().Distance(xyz) < 0.0001)
						{
							result = edge.AsCurve();
							break;
						}
					}
				}
			}
			return result;
		}

		private Curve GetWallGlobalEdge(Duct duct, XYZ wallGlobal)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			XYZ xyz = (hypiping.StartConnector.Origin - hypiping.EndConnector.Origin).Normalize();
			XYZ xyz2 = wallGlobal + 1.0 * xyz;
			return Line.CreateBound(wallGlobal, xyz2);
		}

		public bool DuctRoundBeam(IList<Duct> ducts, IList<XYZ> xyzs, double offSet, double angle, ICollection<ElementId> copyElem)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (ducts.Count < 2 || xyzs.Count < 2)
				{
					throw new System.ArgumentNullException("ducts");
				}
				if (ducts[0].Id != ducts[1].Id)
				{
					throw new System.ArgumentNullException("ducts");
				}
				if (0.0 == offSet)
				{
					result = true;
				}
				else
				{
					double num = Math.Tan(angle);
					if (num < 0.0001)
					{
						num = 0.0;
					}
					double num2 = 0.0;
					if (0.0 != num)
					{
						num2 = Math.Abs(offSet / num);
					}
					if (Math.Abs(num2) < 0.0001)
					{
						num2 = 0.0;
					}
					if (num2 * 2.0 >= xyzs[0].DistanceTo(xyzs[1]))
					{
						result = false;
					}
					else
					{
						Document document = ducts[0].Document;
						YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
						subTransaction = new SubTransaction(document);
						subTransaction.Start();
						if (!hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[0], true)) && !hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[1], true)))
						{
							throw new Exception("选择错误，升降点必须至少有一个在选择的风管上有投影点。");
						}
						if (!hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[0], true)) && hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[1], true)))
						{
							XYZ origin = hypiping.StartConnector.Origin;
							XYZ origin2 = hypiping.EndConnector.Origin;
							DuctType ductType = hypiping.Piping.DuctType;
							XYZ globalOnCenterLinePoint = hypiping.GetGlobalOnCenterLinePoint(xyzs[1]);
							XYZ xyz;
							if (xyzs[0].DistanceTo(origin2) > xyzs[0].DistanceTo(origin))
							{
								xyz = origin;
							}
							else
							{
								xyz = origin2;
							}
							XYZ xyz2 = (xyz - globalOnCenterLinePoint).Normalize();
							XYZ xyz3 = new XYZ();
							XYZ endXYZ = new XYZ();
							xyz3 = globalOnCenterLinePoint + xyz2 * num2 + offSet * XYZ.BasisZ;
							endXYZ = xyz + xyz2 * num2 + offSet * XYZ.BasisZ;
							IList<Duct> list = hypiping.Separate(globalOnCenterLinePoint);
							document.Regenerate();
							Duct duct = YJKRevitTools.NewDuct(list[0], globalOnCenterLinePoint, xyz3);
							Duct duct2 = YJKRevitTools.NewDuct(list[1], xyz3, endXYZ);
							this.ParameterConvert(list[0], duct, document);
							document.Regenerate();
							this.ParameterConvert(list[1], duct2, document);
							document.Regenerate();
							YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct);
							YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct2);
							if (hypiping2.IsZAxis() && hypiping2.IsRect() && hypiping3.IsXAxis())
							{
								hypiping2.Rotate90();
							}
							else if (hypiping2.IsZAxis() && hypiping2.IsRect() && !hypiping3.IsYAxis())
							{
								double xaxisAngle = hypiping3.GetXAxisAngle();
								hypiping2.Rotate(Math.PI*.5 - xaxisAngle);
							}
							HYDuctConnect hyductConnect = new HYDuctConnect();
							double num3 = (list[0].Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (list[0].Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (duct.Location as LocationCurve).Curve.GetEndPoint(0).X;
							double num4 = (duct2.Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (duct2.Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (duct.Location as LocationCurve).Curve.GetEndPoint(0).X;
							Duct item;
							Duct duct3;
							if (num3 * num4 < 0.0)
							{
								item = list[0];
								duct3 = list[1];
							}
							else
							{
								item = list[1];
								duct3 = list[0];
							}
							hyductConnect.NewElbowFitting(new List<Duct>
							{
								item,
								duct
							});
							hyductConnect.NewElbowFitting(new List<Duct>
							{
								duct,
								duct2
							});
							document.Delete(duct3.Id);
							document.Regenerate();
							if (copyElem.Count > 0)
							{
								XYZ hyendPoint = hypiping3.GetLocationCurve().GetEndPoint(0);
								XYZ hyendPoint2 = hypiping3.GetLocationCurve().GetEndPoint(1);
								XYZ middlePoint = this.getMiddlePoint(hyendPoint, hyendPoint2);
								XYZ middlePoint2 = this.getMiddlePoint(hyendPoint, middlePoint);
								XYZ middlePoint3 = this.getMiddlePoint(hyendPoint2, middlePoint);
								IList<Duct> source = hypiping3.Separate(middlePoint2);
								document.Regenerate();
								YJKPiping<Duct> item2 = null;
								YJKPiping<Duct> item3 = null;
								Duct duct4 = null;
								if (this.getMiddlePoint(source.ElementAt(0)).DistanceTo(middlePoint) > hyendPoint.DistanceTo(hyendPoint2) / 4.0)
								{
									item2 = new YJKPiping<Duct>(source.ElementAt(0));
									IList<Duct> source2 = new YJKPiping<Duct>(source.ElementAt(1)).Separate(middlePoint3);
									document.Regenerate();
									if (this.getMiddlePoint(source2.ElementAt(0)).DistanceTo(middlePoint) > hyendPoint.DistanceTo(hyendPoint2) / 4.0)
									{
										item3 = new YJKPiping<Duct>(source2.ElementAt(0));
										duct4 = source2.ElementAt(1);
									}
									else
									{
										item3 = new YJKPiping<Duct>(source2.ElementAt(1));
										duct4 = source2.ElementAt(0);
									}
								}
								else if (this.getMiddlePoint(source.ElementAt(0)).DistanceTo(middlePoint) < hyendPoint.DistanceTo(hyendPoint2) / 4.0)
								{
									item2 = new YJKPiping<Duct>(source.ElementAt(1));
									IList<Duct> source3 = new YJKPiping<Duct>(source.ElementAt(0)).Separate(middlePoint3);
									document.Regenerate();
									if (this.getMiddlePoint(source3.ElementAt(0)).DistanceTo(middlePoint) < hyendPoint.DistanceTo(hyendPoint2) / 4.0)
									{
										item3 = new YJKPiping<Duct>(source3.ElementAt(1));
										duct4 = source3.ElementAt(0);
									}
									else
									{
										item3 = new YJKPiping<Duct>(source3.ElementAt(0));
										duct4 = source3.ElementAt(1);
									}
								}
								List<YJKPiping<Duct>> list2 = new List<YJKPiping<Duct>>();
								list2.Add(item2);
								list2.Add(item3);
								document.Delete(duct4.Id);
								document.Regenerate();
								XYZ xyz4 = new XYZ(-100.0, -100.0, -100.0);
								ElementTransformUtils.MoveElements(document, copyElem, xyz4);
								document.Regenerate();
								ElementTransformUtils.MoveElements(document, copyElem, new XYZ(0.0, 0.0, offSet));
								XYZ xyz5;
								if (copyElem.Count == 1)
								{
									xyz5 = new XYZ(middlePoint.X - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.X, middlePoint.Y - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Y, middlePoint.Z - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Z);
								}
								else
								{
									xyz5 = new XYZ(middlePoint.X - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.X, middlePoint.Y - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Y, middlePoint.Z - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Z);
								}
								document.Regenerate();
								ElementTransformUtils.MoveElements(document, copyElem, xyz5);
								hyductConnect.NewDuctAccessory(list2, middlePoint, copyElem, document);
							}
							document.Regenerate();
							subTransaction.Commit();
							result = true;
						}
						else if (hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[0], true)) && !hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[1], true)))
						{
							XYZ origin3 = hypiping.StartConnector.Origin;
							XYZ origin4 = hypiping.EndConnector.Origin;
							DuctType ductType2 = hypiping.Piping.DuctType;
							XYZ globalOnCenterLinePoint2 = hypiping.GetGlobalOnCenterLinePoint(xyzs[0]);
							XYZ xyz6;
							if (xyzs[1].DistanceTo(origin4) > xyzs[1].DistanceTo(origin3))
							{
								xyz6 = origin3;
							}
							else
							{
								xyz6 = origin4;
							}
							XYZ xyz7 = (xyz6 - globalOnCenterLinePoint2).Normalize();
							XYZ xyz8 = new XYZ();
							XYZ endXYZ2 = new XYZ();
							xyz8 = globalOnCenterLinePoint2 + xyz7 * num2 + offSet * XYZ.BasisZ;
							endXYZ2 = xyz6 + xyz7 * num2 + offSet * XYZ.BasisZ;
							IList<Duct> list3 = hypiping.Separate(globalOnCenterLinePoint2);
							document.Regenerate();
							Duct duct5 = YJKRevitTools.NewDuct(list3[0], globalOnCenterLinePoint2, xyz8);
							Duct duct6 = YJKRevitTools.NewDuct(list3[1], xyz8, endXYZ2);
							this.ParameterConvert(list3[0], duct5, document);
							document.Regenerate();
							this.ParameterConvert(list3[1], duct6, document);
							document.Regenerate();
							YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct5);
							YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct6);
							if (hypiping4.IsZAxis() && hypiping4.IsRect() && hypiping5.IsXAxis())
							{
								hypiping4.Rotate90();
							}
							else if (hypiping4.IsZAxis() && hypiping4.IsRect() && !hypiping5.IsYAxis())
							{
								double xaxisAngle2 = hypiping5.GetXAxisAngle();
								hypiping4.Rotate(Math.PI*.5 - xaxisAngle2);
							}
							HYDuctConnect hyductConnect2 = new HYDuctConnect();
							double num5 = (list3[0].Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (list3[0].Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (duct5.Location as LocationCurve).Curve.GetEndPoint(0).X;
							double num6 = (duct6.Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (duct6.Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (duct5.Location as LocationCurve).Curve.GetEndPoint(0).X;
							Duct item4;
							Duct duct7;
							if (num5 * num6 < 0.0)
							{
								item4 = list3[0];
								duct7 = list3[1];
							}
							else
							{
								item4 = list3[1];
								duct7 = list3[0];
							}
							hyductConnect2.NewElbowFitting(new List<Duct>
							{
								item4,
								duct5
							});
							hyductConnect2.NewElbowFitting(new List<Duct>
							{
								duct5,
								duct6
							});
							document.Delete(duct7.Id);
							document.Regenerate();
							if (copyElem.Count > 0)
							{
								XYZ hyendPoint3 = hypiping5.GetLocationCurve().GetEndPoint(0);
								XYZ hyendPoint4 = hypiping5.GetLocationCurve().GetEndPoint(1);
								XYZ middlePoint4 = this.getMiddlePoint(hyendPoint3, hyendPoint4);
								XYZ middlePoint5 = this.getMiddlePoint(hyendPoint3, middlePoint4);
								XYZ middlePoint6 = this.getMiddlePoint(hyendPoint4, middlePoint4);
								IList<Duct> source4 = hypiping5.Separate(middlePoint5);
								document.Regenerate();
								YJKPiping<Duct> item5 = null;
								YJKPiping<Duct> item6 = null;
								Duct duct8 = null;
								if (this.getMiddlePoint(source4.ElementAt(0)).DistanceTo(middlePoint4) > hyendPoint3.DistanceTo(hyendPoint4) / 4.0)
								{
									item5 = new YJKPiping<Duct>(source4.ElementAt(0));
									IList<Duct> source5 = new YJKPiping<Duct>(source4.ElementAt(1)).Separate(middlePoint6);
									document.Regenerate();
									if (this.getMiddlePoint(source5.ElementAt(0)).DistanceTo(middlePoint4) > hyendPoint3.DistanceTo(hyendPoint4) / 4.0)
									{
										item6 = new YJKPiping<Duct>(source5.ElementAt(0));
										duct8 = source5.ElementAt(1);
									}
									else
									{
										item6 = new YJKPiping<Duct>(source5.ElementAt(1));
										duct8 = source5.ElementAt(0);
									}
								}
								else if (this.getMiddlePoint(source4.ElementAt(0)).DistanceTo(middlePoint4) < hyendPoint3.DistanceTo(hyendPoint4) / 4.0)
								{
									item5 = new YJKPiping<Duct>(source4.ElementAt(1));
									IList<Duct> source6 = new YJKPiping<Duct>(source4.ElementAt(0)).Separate(middlePoint6);
									document.Regenerate();
									if (this.getMiddlePoint(source6.ElementAt(0)).DistanceTo(middlePoint4) < hyendPoint3.DistanceTo(hyendPoint4) / 4.0)
									{
										item6 = new YJKPiping<Duct>(source6.ElementAt(1));
										duct8 = source6.ElementAt(0);
									}
									else
									{
										item6 = new YJKPiping<Duct>(source6.ElementAt(0));
										duct8 = source6.ElementAt(1);
									}
								}
								List<YJKPiping<Duct>> list4 = new List<YJKPiping<Duct>>();
								list4.Add(item5);
								list4.Add(item6);
								document.Delete(duct8.Id);
								document.Regenerate();
								XYZ xyz9 = new XYZ(-100.0, -100.0, -100.0);
								ElementTransformUtils.MoveElements(document, copyElem, xyz9);
								document.Regenerate();
								ElementTransformUtils.MoveElements(document, copyElem, new XYZ(0.0, 0.0, offSet));
								document.Regenerate();
								XYZ xyz10;
								if (copyElem.Count == 1)
								{
									xyz10 = new XYZ(middlePoint4.X - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.X, middlePoint4.Y - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Y, middlePoint4.Z - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Z);
								}
								else
								{
									xyz10 = new XYZ(middlePoint4.X - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.X, middlePoint4.Y - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Y, middlePoint4.Z - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Z);
								}
								document.Regenerate();
								ElementTransformUtils.MoveElements(document, copyElem, xyz10);
								hyductConnect2.NewDuctAccessory(list4, middlePoint4, copyElem, document);
							}
							document.Regenerate();
							subTransaction.Commit();
							result = true;
						}
						else
						{
							XYZ origin5 = hypiping.StartConnector.Origin;
							XYZ origin6 = hypiping.EndConnector.Origin;
							DuctType ductType3 = hypiping.Piping.DuctType;
							XYZ globalOnCenterLinePoint3 = hypiping.GetGlobalOnCenterLinePoint(xyzs[0]);
							XYZ globalOnCenterLinePoint4 = hypiping.GetGlobalOnCenterLinePoint(xyzs[1]);
							XYZ xyz11 = (globalOnCenterLinePoint4 - globalOnCenterLinePoint3).Normalize();
							XYZ xyz12 = new XYZ();
							XYZ xyz13 = new XYZ();
							xyz12 = globalOnCenterLinePoint3 + xyz11 * num2 + offSet * XYZ.BasisZ;
							xyz13 = globalOnCenterLinePoint4 - xyz11 * num2 + offSet * XYZ.BasisZ;
							IList<Duct> list5 = hypiping.Separate(globalOnCenterLinePoint3);
							document.Regenerate();
							YJKPiping<Duct> hypiping6;
							YJKPiping<Duct> hypiping7;
							YJKPiping<Duct> hypiping8;
							if (hypiping.IsPointOnPipingCurve(globalOnCenterLinePoint4))
							{
								hypiping6 = new YJKPiping<Duct>(list5[1]);
								IList<Duct> list6 = hypiping.Separate(globalOnCenterLinePoint4);
								document.Regenerate();
								if (hypiping.StartConnector.Origin.IsAlmostEqualTo(origin5) || hypiping.StartConnector.Origin.IsAlmostEqualTo(origin6) || hypiping.EndConnector.Origin.IsAlmostEqualTo(origin5) || hypiping.EndConnector.Origin.IsAlmostEqualTo(origin6))
								{
									hypiping7 = new YJKPiping<Duct>(list6[0]);
									hypiping8 = new YJKPiping<Duct>(list6[1]);
								}
								else
								{
									hypiping7 = new YJKPiping<Duct>(list6[1]);
									hypiping8 = new YJKPiping<Duct>(list6[0]);
								}
							}
							else
							{
								YJKPiping<Duct> hypiping9 = new YJKPiping<Duct>(list5[1]);
								hypiping6 = new YJKPiping<Duct>(list5[0]);
								IList<Duct> list7 = hypiping9.Separate(globalOnCenterLinePoint4);
								document.Regenerate();
								if (hypiping9.StartConnector.Origin.IsAlmostEqualTo(origin5) || hypiping9.StartConnector.Origin.IsAlmostEqualTo(origin6) || hypiping9.EndConnector.Origin.IsAlmostEqualTo(origin5) || hypiping9.EndConnector.Origin.IsAlmostEqualTo(origin6))
								{
									hypiping7 = new YJKPiping<Duct>(list7[0]);
									hypiping8 = new YJKPiping<Duct>(list7[1]);
								}
								else
								{
									hypiping7 = new YJKPiping<Duct>(list7[1]);
									hypiping8 = new YJKPiping<Duct>(list7[0]);
								}
							}
							document.DeleteElement(hypiping8.Piping);
							document.Regenerate();
							Duct duct9 = YJKRevitTools.NewDuct(hypiping6.Piping, globalOnCenterLinePoint3, xyz12);
							Duct duct10 = YJKRevitTools.NewDuct(hypiping6.Piping, xyz12, xyz13);
							Duct duct11 = YJKRevitTools.NewDuct(hypiping6.Piping, xyz13, globalOnCenterLinePoint4);
							this.ParameterConvert(hypiping6.Piping, duct9, document);
							this.ParameterConvert(hypiping6.Piping, duct10, document);
							this.ParameterConvert(hypiping6.Piping, duct11, document);
							document.Regenerate();
							YJKPiping<Duct> hypiping10 = new YJKPiping<Duct>(duct9);
							YJKPiping<Duct> hypiping11 = new YJKPiping<Duct>(duct10);
							YJKPiping<Duct> hypiping12 = new YJKPiping<Duct>(duct11);
							if (hypiping10.IsZAxis() && hypiping10.IsRect() && hypiping11.IsXAxis())
							{
								hypiping10.Rotate90();
								hypiping12.Rotate90();
							}
							else if (hypiping10.IsZAxis() && hypiping10.IsRect() && !hypiping11.IsYAxis())
							{
								double xaxisAngle3 = hypiping11.GetXAxisAngle();
								hypiping10.Rotate(Math.PI*.5 - xaxisAngle3);
								hypiping12.Rotate(Math.PI*.5 - xaxisAngle3);
							}
							document.Regenerate();
							HYDuctConnect hyductConnect3 = new HYDuctConnect();
							hyductConnect3.NewElbowFitting(new List<Duct>
							{
								hypiping6.Piping,
								hypiping10.Piping
							});
							hyductConnect3.NewElbowFitting(new List<Duct>
							{
								hypiping10.Piping,
								hypiping11.Piping
							});
							hyductConnect3.NewElbowFitting(new List<Duct>
							{
								hypiping11.Piping,
								hypiping12.Piping
							});
							hyductConnect3.NewElbowFitting(new List<Duct>
							{
								hypiping12.Piping,
								hypiping7.Piping
							});
							if (copyElem.Count > 0)
							{
								XYZ hyendPoint5 = hypiping11.GetLocationCurve().GetEndPoint(0);
								XYZ hyendPoint6 = hypiping11.GetLocationCurve().GetEndPoint(1);
								XYZ middlePoint7 = this.getMiddlePoint(hyendPoint5, hyendPoint6);
								XYZ middlePoint8 = this.getMiddlePoint(hyendPoint5, middlePoint7);
								XYZ middlePoint9 = this.getMiddlePoint(hyendPoint6, middlePoint7);
								IList<Duct> source7 = hypiping11.Separate(middlePoint8);
								document.Regenerate();
								YJKPiping<Duct> item7 = null;
								YJKPiping<Duct> item8 = null;
								Duct duct12 = null;
								if (this.getMiddlePoint(source7.ElementAt(0)).DistanceTo(middlePoint7) > hyendPoint5.DistanceTo(hyendPoint6) / 4.0)
								{
									item7 = new YJKPiping<Duct>(source7.ElementAt(0));
									IList<Duct> source8 = new YJKPiping<Duct>(source7.ElementAt(1)).Separate(middlePoint9);
									document.Regenerate();
									if (this.getMiddlePoint(source8.ElementAt(0)).DistanceTo(middlePoint7) > hyendPoint5.DistanceTo(hyendPoint6) / 4.0)
									{
										item8 = new YJKPiping<Duct>(source8.ElementAt(0));
										duct12 = source8.ElementAt(1);
									}
									else
									{
										item8 = new YJKPiping<Duct>(source8.ElementAt(1));
										duct12 = source8.ElementAt(0);
									}
								}
								else if (this.getMiddlePoint(source7.ElementAt(0)).DistanceTo(middlePoint7) < hyendPoint5.DistanceTo(hyendPoint6) / 4.0)
								{
									item7 = new YJKPiping<Duct>(source7.ElementAt(1));
									IList<Duct> source9 = new YJKPiping<Duct>(source7.ElementAt(0)).Separate(middlePoint9);
									document.Regenerate();
									if (this.getMiddlePoint(source9.ElementAt(0)).DistanceTo(middlePoint7) < hyendPoint5.DistanceTo(hyendPoint6) / 4.0)
									{
										item8 = new YJKPiping<Duct>(source9.ElementAt(1));
										duct12 = source9.ElementAt(0);
									}
									else
									{
										item8 = new YJKPiping<Duct>(source9.ElementAt(0));
										duct12 = source9.ElementAt(1);
									}
								}
								List<YJKPiping<Duct>> list8 = new List<YJKPiping<Duct>>();
								list8.Add(item7);
								list8.Add(item8);
								document.Delete(duct12.Id);
								document.Regenerate();
								XYZ xyz14 = new XYZ(-100.0, -100.0, -100.0);
								ElementTransformUtils.MoveElements(document, copyElem, xyz14);
								document.Regenerate();
								ElementTransformUtils.MoveElements(document, copyElem, new XYZ(0.0, 0.0, offSet));
								document.Regenerate();
								XYZ xyz15;
								if (copyElem.Count == 1)
								{
									xyz15 = new XYZ(middlePoint7.X - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.X, middlePoint7.Y - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Y, middlePoint7.Z - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Z);
								}
								else
								{
									xyz15 = new XYZ(middlePoint7.X - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.X, middlePoint7.Y - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Y, middlePoint7.Z - (document.GetElementById(copyElem.ElementAt(0)).Location as LocationPoint).Point.Z);
								}
								document.Regenerate();
								ElementTransformUtils.MoveElements(document, copyElem, xyz15);
								hyductConnect3.NewDuctAccessory(list8, middlePoint7, copyElem, document);
							}
							document.Regenerate();
							subTransaction.Commit();
							result = true;
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null && (int)subTransaction.GetStatus() != 3)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private XYZ getMiddlePoint(XYZ xyzone, XYZ xyztwo)
		{
			return new XYZ((xyzone.X + xyztwo.X) / 2.0, (xyzone.Y + xyztwo.Y) / 2.0, (xyzone.Z + xyztwo.Z) / 2.0);
		}

		private XYZ getMiddlePoint(Duct hyODuct)
		{
			XYZ hyendPoint = (hyODuct.Location as LocationCurve).Curve.GetEndPoint(0);
			XYZ hyendPoint2 = (hyODuct.Location as LocationCurve).Curve.GetEndPoint(1);
			return new XYZ((hyendPoint.X + hyendPoint2.X) / 2.0, (hyendPoint.Y + hyendPoint2.Y) / 2.0, (hyendPoint.Z + hyendPoint2.Z) / 2.0);
		}

		public bool DuctRoundBeam(IList<Duct> ducts, IList<XYZ> xyzs, double offSet, double angle)
		{
			SubTransaction subTransaction = null;
			bool result;
			try
			{
				if (ducts.Count < 2 || xyzs.Count < 2)
				{
					throw new System.ArgumentNullException("ducts");
				}
				if (ducts[0].Id != ducts[1].Id)
				{
					throw new System.ArgumentNullException("ducts");
				}
				if (0.0 == offSet)
				{
					result = true;
				}
				else
				{
					double num = Math.Tan(angle);
					if (num < 0.0001)
					{
						num = 0.0;
					}
					double num2 = 0.0;
					if (0.0 != num)
					{
						num2 = Math.Abs(offSet / num);
					}
					if (Math.Abs(num2) < 0.0001)
					{
						num2 = 0.0;
					}
					if (num2 * 2.0 >= xyzs[0].DistanceTo(xyzs[1]))
					{
						result = false;
					}
					else
					{
						Document document = ducts[0].Document;
						YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
						subTransaction = new SubTransaction(document);
						subTransaction.Start();
						if (!hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[0], true)) && !hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[1], true)))
						{
							throw new Exception("选择错误，升降点必须至少有一个在选择的风管上有投影点。");
						}
						if (!hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[0], true)) && hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[1], true)))
						{
							XYZ origin = hypiping.StartConnector.Origin;
							XYZ origin2 = hypiping.EndConnector.Origin;
							DuctType ductType = hypiping.Piping.DuctType;
							XYZ globalOnCenterLinePoint = hypiping.GetGlobalOnCenterLinePoint(xyzs[1]);
							XYZ xyz;
							if (xyzs[0].DistanceTo(origin2) > xyzs[0].DistanceTo(origin))
							{
								xyz = origin;
							}
							else
							{
								xyz = origin2;
							}
							XYZ xyz2 = (xyz - globalOnCenterLinePoint).Normalize();
							XYZ xyz3 = new XYZ();
							XYZ endXYZ = new XYZ();
							xyz3 = globalOnCenterLinePoint + xyz2 * num2 + offSet * XYZ.BasisZ;
							endXYZ = xyz + xyz2 * num2 + offSet * XYZ.BasisZ;
							IList<Duct> list = hypiping.Separate(globalOnCenterLinePoint);
							document.Regenerate();
							Duct duct = YJKRevitTools.NewDuct(list[0], globalOnCenterLinePoint, xyz3);
							Duct duct2 = YJKRevitTools.NewDuct(list[1], xyz3, endXYZ);
							this.ParameterConvert(list[0], duct, document);
							this.ParameterConvert(list[1], duct2, document);
							YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct);
							YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct2);
							if (hypiping2.IsZAxis() && hypiping2.IsRect() && hypiping3.IsXAxis())
							{
								hypiping2.Rotate90();
							}
							else if (hypiping2.IsZAxis() && hypiping2.IsRect() && !hypiping3.IsYAxis())
							{
								double xaxisAngle = hypiping3.GetXAxisAngle();
								hypiping2.Rotate(Math.PI*.5 - xaxisAngle);
							}
							HYDuctConnect hyductConnect = new HYDuctConnect();
							double num3 = (list[0].Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (list[0].Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (duct.Location as LocationCurve).Curve.GetEndPoint(0).X;
							double num4 = (duct2.Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (duct2.Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (duct.Location as LocationCurve).Curve.GetEndPoint(0).X;
							Duct item;
							Duct duct3;
							if (num3 * num4 < 0.0)
							{
								item = list[0];
								duct3 = list[1];
							}
							else
							{
								item = list[1];
								duct3 = list[0];
							}
							hyductConnect.NewElbowFitting(new List<Duct>
							{
								item,
								duct
							});
							hyductConnect.NewElbowFitting(new List<Duct>
							{
								duct,
								duct2
							});
							document.Delete(duct3.Id);
							subTransaction.Commit();
							document.Regenerate();
							result = true;
						}
						else if (hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[0], true)) && !hypiping.IsPointOnPipingCurve(hypiping.GetProject(xyzs[1], true)))
						{
							XYZ origin3 = hypiping.StartConnector.Origin;
							XYZ origin4 = hypiping.EndConnector.Origin;
							DuctType ductType2 = hypiping.Piping.DuctType;
							XYZ globalOnCenterLinePoint2 = hypiping.GetGlobalOnCenterLinePoint(xyzs[0]);
							XYZ xyz4;
							if (xyzs[1].DistanceTo(origin4) > xyzs[1].DistanceTo(origin3))
							{
								xyz4 = origin3;
							}
							else
							{
								xyz4 = origin4;
							}
							XYZ xyz5 = (xyz4 - globalOnCenterLinePoint2).Normalize();
							XYZ xyz6 = new XYZ();
							XYZ endXYZ2 = new XYZ();
							xyz6 = globalOnCenterLinePoint2 + xyz5 * num2 + offSet * XYZ.BasisZ;
							endXYZ2 = xyz4 + xyz5 * num2 + offSet * XYZ.BasisZ;
							IList<Duct> list2 = hypiping.Separate(globalOnCenterLinePoint2);
							document.Regenerate();
							Duct duct4 = YJKRevitTools.NewDuct(list2[0], globalOnCenterLinePoint2, xyz6);
							Duct duct5 = YJKRevitTools.NewDuct(list2[1], xyz6, endXYZ2);
							this.ParameterConvert(list2[0], duct4, document);
							this.ParameterConvert(list2[1], duct5, document);
							YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct4);
							YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct5);
							if (hypiping4.IsZAxis() && hypiping4.IsRect() && hypiping5.IsXAxis())
							{
								hypiping4.Rotate90();
							}
							else if (hypiping4.IsZAxis() && hypiping4.IsRect() && !hypiping5.IsYAxis())
							{
								double xaxisAngle2 = hypiping5.GetXAxisAngle();
								hypiping4.Rotate(Math.PI*.5 - xaxisAngle2);
							}
							HYDuctConnect hyductConnect2 = new HYDuctConnect();
							double num5 = (list2[0].Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (list2[0].Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (duct4.Location as LocationCurve).Curve.GetEndPoint(0).X;
							double num6 = (duct5.Location as LocationCurve).Curve.GetEndPoint(0).X / 2.0 + (duct5.Location as LocationCurve).Curve.GetEndPoint(1).X / 2.0 - (duct4.Location as LocationCurve).Curve.GetEndPoint(0).X;
							Duct item2;
							Duct duct6;
							if (num5 * num6 < 0.0)
							{
								item2 = list2[0];
								duct6 = list2[1];
							}
							else
							{
								item2 = list2[1];
								duct6 = list2[0];
							}
							hyductConnect2.NewElbowFitting(new List<Duct>
							{
								item2,
								duct4
							});
							hyductConnect2.NewElbowFitting(new List<Duct>
							{
								duct4,
								duct5
							});
							document.Delete(duct6.Id);
							subTransaction.Commit();
							document.Regenerate();
							result = true;
						}
						else
						{
							XYZ origin5 = hypiping.StartConnector.Origin;
							XYZ origin6 = hypiping.EndConnector.Origin;
							DuctType ductType3 = hypiping.Piping.DuctType;
							XYZ globalOnCenterLinePoint3 = hypiping.GetGlobalOnCenterLinePoint(xyzs[0]);
							XYZ globalOnCenterLinePoint4 = hypiping.GetGlobalOnCenterLinePoint(xyzs[1]);
							XYZ xyz7 = (globalOnCenterLinePoint4 - globalOnCenterLinePoint3).Normalize();
							XYZ xyz8 = new XYZ();
							XYZ xyz9 = new XYZ();
							xyz8 = globalOnCenterLinePoint3 + xyz7 * num2 + offSet * XYZ.BasisZ;
							xyz9 = globalOnCenterLinePoint4 - xyz7 * num2 + offSet * XYZ.BasisZ;
							IList<Duct> list3 = hypiping.Separate(globalOnCenterLinePoint3);
							document.Regenerate();
							YJKPiping<Duct> hypiping6;
							YJKPiping<Duct> hypiping7;
							YJKPiping<Duct> hypiping8;
							if (hypiping.IsPointOnPipingCurve(globalOnCenterLinePoint4))
							{
								hypiping6 = new YJKPiping<Duct>(list3[1]);
								IList<Duct> list4 = hypiping.Separate(globalOnCenterLinePoint4);
								document.Regenerate();
								if (hypiping.StartConnector.Origin.IsAlmostEqualTo(origin5) || hypiping.StartConnector.Origin.IsAlmostEqualTo(origin6) || hypiping.EndConnector.Origin.IsAlmostEqualTo(origin5) || hypiping.EndConnector.Origin.IsAlmostEqualTo(origin6))
								{
									hypiping7 = new YJKPiping<Duct>(list4[0]);
									hypiping8 = new YJKPiping<Duct>(list4[1]);
								}
								else
								{
									hypiping7 = new YJKPiping<Duct>(list4[1]);
									hypiping8 = new YJKPiping<Duct>(list4[0]);
								}
							}
							else
							{
								YJKPiping<Duct> hypiping9 = new YJKPiping<Duct>(list3[1]);
								hypiping6 = new YJKPiping<Duct>(list3[0]);
								IList<Duct> list5 = hypiping9.Separate(globalOnCenterLinePoint4);
								document.Regenerate();
								if (hypiping9.StartConnector.Origin.IsAlmostEqualTo(origin5) || hypiping9.StartConnector.Origin.IsAlmostEqualTo(origin6) || hypiping9.EndConnector.Origin.IsAlmostEqualTo(origin5) || hypiping9.EndConnector.Origin.IsAlmostEqualTo(origin6))
								{
									hypiping7 = new YJKPiping<Duct>(list5[0]);
									hypiping8 = new YJKPiping<Duct>(list5[1]);
								}
								else
								{
									hypiping7 = new YJKPiping<Duct>(list5[1]);
									hypiping8 = new YJKPiping<Duct>(list5[0]);
								}
							}
							document.DeleteElement(hypiping8.Piping);
							document.Regenerate();
							Duct duct7 = YJKRevitTools.NewDuct(hypiping6.Piping, globalOnCenterLinePoint3, xyz8);
							Duct duct8 = YJKRevitTools.NewDuct(hypiping6.Piping, xyz8, xyz9);
							Duct duct9 = YJKRevitTools.NewDuct(hypiping6.Piping, xyz9, globalOnCenterLinePoint4);
							this.ParameterConvert(hypiping6.Piping, duct7, document);
							this.ParameterConvert(hypiping6.Piping, duct8, document);
							this.ParameterConvert(hypiping6.Piping, duct9, document);
							document.Regenerate();
							YJKPiping<Duct> hypiping10 = new YJKPiping<Duct>(duct7);
							YJKPiping<Duct> hypiping11 = new YJKPiping<Duct>(duct8);
							YJKPiping<Duct> hypiping12 = new YJKPiping<Duct>(duct9);
							if (hypiping10.IsZAxis() && hypiping10.IsRect() && hypiping11.IsXAxis())
							{
								hypiping10.Rotate90();
								hypiping12.Rotate90();
							}
							else if (hypiping10.IsZAxis() && hypiping10.IsRect() && !hypiping11.IsYAxis())
							{
								double xaxisAngle3 = hypiping11.GetXAxisAngle();
								hypiping10.Rotate(Math.PI*.5 - xaxisAngle3);
								hypiping12.Rotate(Math.PI*.5 - xaxisAngle3);
							}
							document.Regenerate();
							HYDuctConnect hyductConnect3 = new HYDuctConnect();
							hyductConnect3.NewElbowFitting(new List<Duct>
							{
								hypiping6.Piping,
								hypiping10.Piping
							});
							hyductConnect3.NewElbowFitting(new List<Duct>
							{
								hypiping10.Piping,
								hypiping11.Piping
							});
							hyductConnect3.NewElbowFitting(new List<Duct>
							{
								hypiping11.Piping,
								hypiping12.Piping
							});
							hyductConnect3.NewElbowFitting(new List<Duct>
							{
								hypiping12.Piping,
								hypiping7.Piping
							});
							document.Regenerate();
							subTransaction.Commit();
							document.Regenerate();
							result = true;
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null && (int)subTransaction.GetStatus() != 3)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private void ParameterConvert(Duct sourceDuct, Duct newDuct, Autodesk.Revit.DB.Document document)
		{
			SubTransaction subTransaction = new SubTransaction(document);
			subTransaction.Start();
			foreach (object obj in newDuct.Parameters)
			{
				Parameter parameter = (Parameter)obj;
                switch ((int)parameter.StorageType)
				{
				case 1:
					if (!parameter.IsReadOnly)
					{
						parameter.Set(sourceDuct.get_Parameter(parameter.Definition).AsInteger());
					}
					break;
				case 2:
					if (parameter.Id.IntegerValue != -1114132 && !parameter.IsReadOnly)
					{
						parameter.Set(sourceDuct.get_Parameter(parameter.Definition).AsDouble());
					}
					break;
				case 3:
					if (!parameter.IsReadOnly && parameter.IsShared)
					{
						parameter.Set(sourceDuct.get_Parameter(parameter.Definition).AsString());
					}
					break;
				case 4:
					parameter.AsElementId();
					if (!parameter.IsReadOnly)
					{
						parameter.Set(sourceDuct.get_Parameter(parameter.Definition).AsElementId());
					}
					break;
				}
			}
            if ((int)subTransaction.Commit() == 5)
			{
				subTransaction.RollBack();
			}
		}

		private List<ElementNode> GetAllElemNodeSJPY(ElementNode eNode, ref List<ElementNode> listENode)
		{
			if (eNode == null)
			{
				return listENode;
			}
			listENode.Add(eNode);
			this.GetAllElemNode(eNode.Next, ref listENode);
			if (eNode.SubElementNode != null)
			{
				foreach (ElementNode eNode2 in eNode.SubElementNode)
				{
					this.GetAllElemNode(eNode2, ref listENode);
				}
			}
			return listENode;
		}

		public void NewPipePIANYI(Duct pipe, XYZ xyz1, Duct pipe2, XYZ xyz2, double distance, double angle, string model)
		{
			Document document = pipe.Document;
			if (model == "升降")
			{
				if (Math.Abs(distance) < 3.0 * this.getDuctHeight(pipe))
				{
					DialogResult dialogResult = this.IsDistanceOK("升降高度");
					if (dialogResult == DialogResult.Yes)
					{
						if (distance < 0.0)
						{
							distance = -3.0 * this.getDuctHeight(pipe);
						}
						else
						{
							distance = 3.0 * this.getDuctHeight(pipe);
						}
					}
					else if (dialogResult != DialogResult.No)
					{
						return;
					}
				}
			}
			else if (Math.Abs(distance) < 3.0 * this.getDuctWidth(pipe))
			{
				DialogResult dialogResult2 = this.IsDistanceOK("偏移距离");
				if (dialogResult2 == DialogResult.Yes)
				{
					if (distance < 0.0)
					{
						distance = -3.0 * this.getDuctWidth(pipe);
					}
					else
					{
						distance = 3.0 * this.getDuctWidth(pipe);
					}
				}
				else if (dialogResult2 != DialogResult.No)
				{
					return;
				}
			}
			SubTransaction subTransaction = new SubTransaction(document);
			try
			{
				subTransaction.Start();
				if (pipe.Id != pipe2.Id)
				{
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(pipe, xyz1);
					YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(pipe2, xyz2);
					XYZ project = hypiping.GetProject(xyz1);
					XYZ project2 = hypiping2.GetProject(xyz2);
					XYZ point = new XYZ(100.0, 100.0, 100.0);
					hypiping.GetProject(point, true);
					XYZ xyz3 = hypiping.StartConnector.Connector.CoordinateSystem.BasisX.Normalize();
					XYZ point2 = hypiping.StartConnector.Connector.Origin + xyz3 * 10.0;
					if (!new HYCreatePipeHelper().judgePointIsAnotherLine(hypiping.GetLocationCurve() as Line, point2))
					{
						xyz3 = -1.0 * xyz3.Normalize();
					}
					XYZ xyz4 = null;
					if (hypiping.IsParallel(hypiping2.Piping))
					{
						if (model == "偏移")
						{
							if (distance > 0.0)
							{
								xyz4 = xyz3;
							}
							else
							{
								xyz4 = -1.0 * xyz3;
							}
						}
						if (model == "升降")
						{
							if (distance > 0.0)
							{
								xyz4 = new XYZ(0.0, 0.0, 1.0);
							}
							else
							{
								xyz4 = new XYZ(0.0, 0.0, -1.0);
							}
						}
					}
					else
					{
						if (!(model == "升降"))
						{
							YJKMessageBox.Show("两根管道不平行时候不提供偏移功能！");
							return;
						}
						if (distance > 0.0)
						{
							xyz4 = new XYZ(0.0, 0.0, 1.0);
						}
						else
						{
							xyz4 = new XYZ(0.0, 0.0, -1.0);
						}
					}
					List<Duct> source = hypiping.Separate(project);
					document.Regenerate();
					List<Duct> source2 = hypiping2.Separate(project2);
					document.Regenerate();
					Duct duct;
					Duct duct2;
					if (this.getMiddlePoint(source.ElementAt(0)).DistanceTo(project) + this.getMiddlePoint(source.ElementAt(0)).DistanceTo(project2) > this.getMiddlePoint(source.ElementAt(1)).DistanceTo(project) + this.getMiddlePoint(source.ElementAt(1)).DistanceTo(project2))
					{
						duct = source.ElementAt(0);
						duct2 = source.ElementAt(1);
					}
					else
					{
						duct = source.ElementAt(1);
						duct2 = source.ElementAt(0);
					}
					Duct duct3;
					Duct duct4;
					if (this.getMiddlePoint(source2.ElementAt(0)).DistanceTo(project2) + this.getMiddlePoint(source2.ElementAt(0)).DistanceTo(project) > this.getMiddlePoint(source2.ElementAt(1)).DistanceTo(project2) + this.getMiddlePoint(source2.ElementAt(1)).DistanceTo(project))
					{
						duct3 = source2.ElementAt(0);
						duct4 = source2.ElementAt(1);
					}
					else
					{
						duct3 = source2.ElementAt(1);
						duct4 = source2.ElementAt(0);
					}
					List<ElementId> list = new List<ElementId>();
					list.Add(duct2.Id);
					list.Add(duct4.Id);
					YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct3);
					distance = Math.Abs(distance) + hypiping3.GetHeightOrDiameter();
					document.Regenerate();
					ElementTransformUtils.MoveElements(document, list, xyz4 * Math.Abs(distance));
					document.Regenerate();
					XYZ xyz5;
					XYZ xyz6;
					Duct duct5;
					Duct duct6;
					if (angle == Math.PI*.5)
					{
						xyz5 = project + xyz4 * Math.Abs(distance);
						xyz6 = project2 + xyz4 * Math.Abs(distance);
						duct5 = YJKRevitTools.NewDuct(duct, project, xyz5);
						document.Regenerate();
						duct6 = YJKRevitTools.NewDuct(duct3, project2, xyz6);
						document.Regenerate();
					}
					else
					{
						xyz5 = project + xyz4 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * YJKRevitTools.getLocationCurveDirectionfarAwayfromXYZ(duct2, project).Normalize();
						xyz6 = project2 + xyz4 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * YJKRevitTools.getLocationCurveDirectionfarAwayfromXYZ(duct4, project2).Normalize();
						duct5 = YJKRevitTools.NewDuct(duct, project, xyz5);
						document.Regenerate();
						duct6 = YJKRevitTools.NewDuct(duct3, project2, xyz6);
						document.Regenerate();
					}
					YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct5);
					YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct2);
					YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(duct4);
					YJKPiping<Duct> hypiping7 = new YJKPiping<Duct>(duct6);
					if (hypiping4.IsZAxis() && hypiping4.IsRect() && hypiping5.IsXAxis() && hypiping6.IsXAxis())
					{
						hypiping4.Rotate90();
						hypiping7.Rotate90();
					}
					else if (hypiping4.IsZAxis() && hypiping4.IsRect() && !hypiping5.IsYAxis() && !hypiping6.IsYAxis())
					{
						double xaxisAngle = hypiping5.GetXAxisAngle();
						double xaxisAngle2 = hypiping6.GetXAxisAngle();
						hypiping4.Rotate(Math.PI*.5 - xaxisAngle);
						hypiping7.Rotate(Math.PI*.5 - xaxisAngle2);
					}
					document.Regenerate();
					YJKPiping<Duct> hypiping8 = new YJKPiping<Duct>(duct2);
					YJKPiping<Duct> hypiping9 = new YJKPiping<Duct>(duct4);
					if ((!hypiping8.IsPointOnPipingCurve(xyz5) || !hypiping9.IsPointOnPipingCurve(xyz6)) && MessageBox.Show("由于偏移角度过小或者其他原因可能造成连接出错，是否强行执行？", "盈建科提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) != DialogResult.Yes)
					{
						subTransaction.RollBack();
					}
					else
					{
						new HYDuctConnect();
						document.Regenerate();
						this.ElbowConnect(duct, duct5, project, project);
						this.ElbowConnect(duct5, duct2, xyz5, project);
						this.ElbowConnect(duct3, duct6, project2, project2);
						this.ElbowConnect(duct6, duct4, xyz6, project2);
						document.Regenerate();
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.EndTransation(subTransaction, false);
			}
			finally
			{
				this.EndTransation(subTransaction, true);
			}
		}

		public bool NewPipePIANYI(Duct pipe, XYZ xyz1, XYZ xyz2, double distance, double angle, string model, bool isDirect = false)
		{
			ElementId id = pipe.Id;
			bool result = false;
			Document document = pipe.Document;
			if (!isDirect)
			{
				if (model == "升降")
				{
					if (Math.Abs(distance) < 3.0 * this.getDuctHeight(pipe))
					{
						DialogResult dialogResult = this.IsDistanceOK("升降高度");
						if (dialogResult == DialogResult.Yes)
						{
							if (distance < 0.0)
							{
								distance = -3.0 * this.getDuctHeight(pipe);
							}
							else
							{
								distance = 3.0 * this.getDuctHeight(pipe);
							}
						}
						else if (dialogResult != DialogResult.No)
						{
							return false;
						}
					}
				}
				else if (Math.Abs(distance) < 3.0 * this.getDuctWidth(pipe))
				{
					DialogResult dialogResult2 = this.IsDistanceOK("偏移距离");
					if (dialogResult2 == DialogResult.Yes)
					{
						if (distance < 0.0)
						{
							distance = -3.0 * this.getDuctWidth(pipe);
						}
						else
						{
							distance = 3.0 * this.getDuctWidth(pipe);
						}
					}
					else if (dialogResult2 != DialogResult.No)
					{
						return false;
					}
				}
			}
			SubTransaction subTransaction = new SubTransaction(document);
			try
			{
				subTransaction.Start();
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(pipe);
				XYZ xyz3 = hypiping.StartConnector.Connector.CoordinateSystem.BasisX.Normalize();
				XYZ point = hypiping.StartConnector.Connector.Origin + xyz3 * 10.0;
				if (!new HYCreatePipeHelper().judgePointIsAnotherLine(hypiping.GetLocationCurve() as Line, point))
				{
					xyz3 = -1.0 * xyz3.Normalize();
				}
				XYZ project = hypiping.GetProject(xyz1);
				XYZ project2 = hypiping.GetProject(xyz2);
				List<Duct> source = new List<Duct>();
				source = hypiping.Separate(project);
				document.Regenerate();
				new YJKPiping<Duct>(source.ElementAt(0));
				Duct duct;
				Duct piping;
				if (new YJKPiping<Duct>(source.ElementAt(1)).IsPointOnPipingCurve(project2, 1E-06))
				{
					duct = source.ElementAt(0);
					piping = source.ElementAt(1);
				}
				else
				{
					duct = source.ElementAt(1);
					piping = source.ElementAt(0);
				}
				List<Duct> source2 = new List<Duct>();
				source2 = new YJKPiping<Duct>(piping).Separate(project2);
				document.Regenerate();
				Duct pipe2;
				Duct duct2;
				if (this.getMiddlePoint(source2.ElementAt(0)).DistanceTo(project2) + this.getMiddlePoint(source2.ElementAt(0)).DistanceTo(project) > this.getMiddlePoint(source2.ElementAt(1)).DistanceTo(project2) + this.getMiddlePoint(source2.ElementAt(1)).DistanceTo(project))
				{
					pipe2 = source2.ElementAt(0);
					duct2 = source2.ElementAt(1);
				}
				else
				{
					pipe2 = source2.ElementAt(1);
					duct2 = source2.ElementAt(0);
				}
				XYZ xyz4 = null;
				if (model == "偏移")
				{
					if (distance > 0.0)
					{
						xyz4 = xyz3;
					}
					else
					{
						xyz4 = -1.0 * xyz3;
					}
				}
				if (model == "升降")
				{
					if (distance > 0.0)
					{
						xyz4 = new XYZ(0.0, 0.0, 1.0);
					}
					else
					{
						xyz4 = new XYZ(0.0, 0.0, -1.0);
					}
				}
				new YJKPiping<Duct>(duct2);
				distance = Math.Abs(distance);
				document.Regenerate();
				ElementTransformUtils.MoveElement(document, duct2.Id, xyz4 * Math.Abs(distance));
				document.Regenerate();
				XYZ xyz5;
				XYZ xyz6;
				Duct duct3;
				Duct duct4;
				if (angle == Math.PI*.5)
				{
					xyz5 = project + xyz4 * Math.Abs(distance);
					xyz6 = project2 + xyz4 * Math.Abs(distance);
					duct3 = YJKRevitTools.NewDuct(duct, project, xyz5);
					duct4 = YJKRevitTools.NewDuct(duct, project2, xyz6);
					document.Regenerate();
				}
				else
				{
					xyz5 = project + xyz4 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * (project2 - project).Normalize();
					xyz6 = project2 + xyz4 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * (project - project2).Normalize();
					duct3 = YJKRevitTools.NewDuct(duct, project, xyz5);
					duct4 = YJKRevitTools.NewDuct(duct, project2, xyz6);
					document.Regenerate();
				}
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct3);
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct2);
				YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(duct4);
				if (hypiping2.IsZAxis() && hypiping2.IsRect() && hypiping3.IsXAxis())
				{
					hypiping2.Rotate90();
					hypiping4.Rotate90();
				}
				else if (hypiping2.IsZAxis() && hypiping2.IsRect() && !hypiping3.IsYAxis())
				{
					double xaxisAngle = hypiping3.GetXAxisAngle();
					hypiping2.Rotate(Math.PI*.5 - xaxisAngle);
					hypiping4.Rotate(Math.PI*.5 - xaxisAngle);
				}
				document.Regenerate();
				YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(duct2);
				XYZ xyz7 = (hypiping5.EndConnector.Origin + hypiping5.StartConnector.Origin) / 2.0;
				XYZ xyz8 = project + xyz4 * Math.Abs(distance);
				if (xyz5.DistanceTo(xyz7) + xyz8.DistanceTo(xyz5) > xyz8.DistanceTo(xyz7))
				{
					if (new FormPYSJWrong("shuangce").ShowDialog() == DialogResult.OK)
					{
						subTransaction.RollBack();
						document.Regenerate();
						return this.NewPipePIANYI(document.GetElementById(id) as Duct, xyz1, xyz2, distance, Math.PI*.5, model, false);
					}
					subTransaction.RollBack();
					return false;
				}
				else
				{
					new HYDuctConnect();
					document.Regenerate();
					this.ElbowConnect(duct, duct3, project, project);
					this.ElbowConnect(duct3, duct2, xyz5, project);
					this.ElbowConnect(duct2, duct4, project2, xyz6);
					this.ElbowConnect(duct4, pipe2, project2, project2);
					document.Regenerate();
					result = true;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.EndTransation(subTransaction, false);
			}
			finally
			{
				this.EndTransation(subTransaction, true);
			}
			return result;
		}

		public bool NewPipePIANYI(Duct pipe, XYZ xyz1, double distance, double angle, string model, XYZ xyzDirection)
		{
			ElementId id = pipe.Id;
			bool result = false;
			Document document = pipe.Document;
			if (model == "升降")
			{
				if (Math.Abs(distance) < 3.0 * this.getDuctHeight(pipe))
				{
					DialogResult dialogResult = this.IsDistanceOK("升降高度");
					if (dialogResult == DialogResult.Yes)
					{
						if (distance < 0.0)
						{
							distance = -3.0 * this.getDuctHeight(pipe);
						}
						else
						{
							distance = 3.0 * this.getDuctHeight(pipe);
						}
					}
					else if (dialogResult != DialogResult.No)
					{
						return false;
					}
				}
			}
			else if (Math.Abs(distance) < 3.0 * this.getDuctWidth(pipe))
			{
				DialogResult dialogResult2 = this.IsDistanceOK("偏移距离");
				if (dialogResult2 == DialogResult.Yes)
				{
					if (distance < 0.0)
					{
						distance = -3.0 * this.getDuctWidth(pipe);
					}
					else
					{
						distance = 3.0 * this.getDuctWidth(pipe);
					}
				}
				else if (dialogResult2 != DialogResult.No)
				{
					return false;
				}
			}
			SubTransaction subTransaction = new SubTransaction(document);
			try
			{
				subTransaction.Start();
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(pipe);
				XYZ xyz2 = hypiping.StartConnector.Connector.CoordinateSystem.BasisX.Normalize();
				XYZ point = hypiping.StartConnector.Connector.Origin + xyz2 * 10.0;
				if (!new HYCreatePipeHelper().judgePointIsAnotherLine(hypiping.GetLocationCurve() as Line, point))
				{
					xyz2 = -1.0 * xyz2.Normalize();
				}
				XYZ project = hypiping.GetProject(xyz1);
				XYZ project2 = hypiping.GetProject(xyzDirection, true);
				List<Duct> source = new List<Duct>();
				source = hypiping.Separate(project);
				document.Regenerate();
				new YJKPiping<Duct>(source.ElementAt(0));
				Duct duct;
				Duct duct2;
				if (new YJKPiping<Duct>(source.ElementAt(1)).IsPointOnPipingCurve(project2, 1E-06))
				{
					duct = source.ElementAt(0);
					duct2 = source.ElementAt(1);
				}
				else
				{
					duct = source.ElementAt(1);
					duct2 = source.ElementAt(0);
				}
				XYZ xyz3 = null;
				if (model == "偏移")
				{
					if (distance > 0.0)
					{
						xyz3 = xyz2;
					}
					else
					{
						xyz3 = -1.0 * xyz2;
					}
				}
				if (model == "升降")
				{
					if (distance > 0.0)
					{
						xyz3 = new XYZ(0.0, 0.0, 1.0);
					}
					else
					{
						xyz3 = new XYZ(0.0, 0.0, -1.0);
					}
				}
				new YJKPiping<Duct>(duct2);
				distance = Math.Abs(distance);
				document.Regenerate();
				ElementTransformUtils.MoveElement(document, duct2.Id, xyz3 * Math.Abs(distance));
				document.Regenerate();
				XYZ xyz4;
				Duct duct3;
				if (angle == Math.PI*.5)
				{
					xyz4 = project + xyz3 * Math.Abs(distance);
					duct3 = YJKRevitTools.NewDuct(duct, project, xyz4);
					document.Regenerate();
				}
				else
				{
					xyz4 = project + xyz3 * Math.Abs(distance) + Math.Abs(distance) / Math.Tan(angle) * (project2 - project).Normalize();
					duct3 = YJKRevitTools.NewDuct(duct, project, xyz4);
					document.Regenerate();
				}
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct3);
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct2);
				if (hypiping2.IsZAxis() && hypiping2.IsRect() && hypiping3.IsXAxis())
				{
					hypiping2.Rotate90();
				}
				else if (hypiping2.IsZAxis() && hypiping2.IsRect() && !hypiping3.IsYAxis())
				{
					double xaxisAngle = hypiping3.GetXAxisAngle();
					hypiping2.Rotate(Math.PI*.5 - xaxisAngle);
				}
				document.Regenerate();
				if (!new YJKPiping<Duct>(duct2).IsPointOnPipingCurve(xyz4))
				{
					if (new FormPYSJWrong("dance").ShowDialog() == DialogResult.OK)
					{
						subTransaction.RollBack();
						document.Regenerate();
						return this.NewPipePIANYI(document.GetElementById(id) as Duct, xyz1, distance, Math.PI*.5, model, xyzDirection);
					}
					subTransaction.RollBack();
					return false;
				}
				else
				{
					new HYDuctConnect();
					document.Regenerate();
					this.ElbowConnect(duct, duct3, project, project);
					this.ElbowConnect(duct3, duct2, xyz4, project);
					document.Regenerate();
					result = true;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.EndTransation(subTransaction, false);
			}
			finally
			{
				this.EndTransation(subTransaction, true);
			}
			return result;
		}

		private void ElbowConnect(Duct pipe1, Duct pipe2, XYZ xyz1, XYZ xyz2)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(pipe1);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(pipe2);
			pipe1.Document.Create.NewElbowFitting(hypiping.GetCloseConnector(xyz1, false), hypiping2.GetCloseConnector(xyz2, false));
		}

		private void EndTransation(SubTransaction tran, bool beSure)
		{
			if (tran != null)
			{
				TransactionStatus status = tran.GetStatus();
				if ((int)status == 1)
				{
					if (beSure)
					{
						tran.Commit();
						return;
					}
					tran.RollBack();
					return;
				}
				else if ((int)status == 5)
				{
					tran.RollBack();
				}
			}
		}
	}
}
